package com.example.hw;



import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout.*;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class ActMain_Hw extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actmain_hw);
		InitialComponet();
		ShopFactory sf = new ShopFactory();
		//第一次使用，請把以下3個註解拿掉初始，之後再註解掉 重開
		
		//sf.goDelete(ActMain_Hw.this);
		//sf.goCreateTable(ActMain_Hw.this);
		//sf.goInsertProduct(ActMain_Hw.this);
		showOrderTable();
	}
	
	public void showOrderTable(){
		ShopFactory sf = new ShopFactory();
		String data[][] = sf.goQueryOrders(ActMain_Hw.this);
		//設定成為可以拉伸
		mainTable.setStretchAllColumns(true);

		for(int i = 0 ; i < data.length ; i ++){
			TableRow tr = new TableRow(this);
			
			Button row1 = new Button(this);
			TextView row2 = new TextView(this);
			
			row1.setLayoutParams(new TableRow.LayoutParams
					(TableRow.LayoutParams.WRAP_CONTENT, 
					 TableRow.LayoutParams.WRAP_CONTENT));
			row2.setLayoutParams(new TableRow.LayoutParams
					(TableRow.LayoutParams.WRAP_CONTENT, 
					 TableRow.LayoutParams.WRAP_CONTENT));
			//設定置中
			row1.setGravity(Gravity.CENTER);
			row2.setGravity(Gravity.CENTER);
			row1.setText(data[i][0]);
			row2.setText(data[i][1]);
		
			row1.setOnClickListener(btnListDrink);
			
			tr.addView(row1);
			tr.addView(row2);
			//System.out.println("ID="+data[i][0]+" date="+data[i][1]);
			mainTable.addView(tr, new TableLayout.LayoutParams
					(TableLayout.LayoutParams.WRAP_CONTENT,
					 TableLayout.LayoutParams.WRAP_CONTENT));
		}
		if(data.length!=0){
			Toast.makeText(ActMain_Hw.this, "最新一筆訂單 "+
					data[0][0], Toast.LENGTH_LONG).show();
		}
	}
	OnClickListener btnListDrink = new OnClickListener() {
		public void onClick(View v) {		
			Builder build=new Builder(ActMain_Hw.this);
			build.setTitle("飲料訂購：");
			ShopFactory sf = new ShopFactory();
			int order_id = Integer.parseInt(((Button) v).getText().toString());
			System.out.println("單號="+order_id);
			String data[][] = sf.goQueryOrder(ActMain_Hw.this, order_id);
			System.out.println(data[0][0]);
			String list[][] = new String[data.length][4];
			
			String drinkName[] = new String[data.length];
			String show[] = new String[data.length+1];
			for(int i = 0 ; i<data.length ;i++){
				int key = Integer.parseInt(data[i][2]);//key隨著Data改變
				drinkName[i]=sf.goQueryProducts(ActMain_Hw.this, key)[1];
			}
			show[0] = "項目\t飲料\t單價\t數量";
					
			for(int i = 0 ; i<data.length ;i++){
				list[i][0]=data[i][0];	//項次
				list[i][1]=drinkName[i];//飲料
				list[i][2]=data[i][4];	//單價
				list[i][3]=data[i][3];	//數量
				
				show[i+1] = list[i][0]+"\t\t"+list[i][1]+"\t\t"+list[i][2]+"\t\t"
							+list[i][3];
				System.out.println(list[i][0]+"\t"+list[i][1]+"\t"+list[i][2]+"\t"
							+list[i][3]);
			}
			
			build.setItems(show, onSelect_click);
			Dialog dlg=build.create();
			dlg.show();			
		}
	};
	
	DialogInterface.OnClickListener onSelect_click=new DialogInterface.OnClickListener(){
		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			
	}};
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.act_main__hw, menu);
		return true;
	}
	
	
	OnClickListener btn_goOrder = new OnClickListener() {
		public void onClick(View v) {		
			//new一個Intent去呼叫新的Act2
			Intent intent = new Intent(ActMain_Hw.this,ActOrder.class);
			//去啟動新的Intent Activity
			startActivity(intent);		
		}
	};
	
	OnClickListener btn_showAboutMe = new OnClickListener() {
		public void onClick(View v) {	
			String[] myStr = {"由72期朱東輝製作"};
			Builder build=new Builder(ActMain_Hw.this);
			build.setTitle("APP 作業");
			build.setItems(myStr, null);
			Dialog dlg=build.create();
			dlg.show();	
		}
	};
	
	OnClickListener btn_DrinkSet_click = new OnClickListener() {
		public void onClick(View v) {
			
			//new一個Intent去呼叫飲料設定
			Intent intent = new Intent(ActMain_Hw.this,ActDrink.class);
			//去啟動新的Intent Activity
			startActivity(intent);	
		}
	};


	TableLayout mainTable 	= null;
	Button btnOrder 		= null;
	Button btnAboutMe		= null;
	Button btnDrinkSet		= null;
	
	private void InitialComponet() {
		btnOrder = (Button)findViewById(R.id.btnOrder);
		btnOrder.setOnClickListener(btn_goOrder);
		
		btnAboutMe = (Button)findViewById(R.id.btnAboutMe);
		btnAboutMe.setOnClickListener(btn_showAboutMe);
		
		btnDrinkSet = (Button)findViewById(R.id.btnDrinkSet);
		btnDrinkSet.setOnClickListener(btn_DrinkSet_click);
		//主頁面表單
		mainTable = (TableLayout)findViewById(R.id.mainTable);	
	}
}
